package com.shqcjd.generalplatformprojectclient.ui.presenter;

import android.os.Handler;

import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxObserver;
import com.shqcjd.generalplatformprojectclient.api.ApiService;
import com.shqcjd.generalplatformprojectclient.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.generalplatformprojectclient.model.ResponseData;
import com.shqcjd.generalplatformprojectclient.model.pojo.LoginBean;
import com.shqcjd.generalplatformprojectclient.ui.base.BasePresenter;
import com.shqcjd.generalplatformprojectclient.ui.view.LoginRegistView;

import io.reactivex.disposables.Disposable;
/**
 * 登录注册
 * Created by sy on 2018/12/10.
 */

public class LoginRegistPresenter extends BasePresenter<LoginRegistView> {
    private final int CODE_TIME = 1000;
    private int codeNum = 60;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            getView().getCodeTime(codeNum);
            if(codeNum==1){
                getView().getCodeEnd();
                handler.removeCallbacks(runnable);
            }else{
                codeNum--;
                handler.postDelayed(runnable, CODE_TIME);
            }
        }
    };


    public void toLogin(String username, String password) {
        ApiService.login(username,password, 2)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<LoginBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        getView().showProgress("正在登录...");
                    }

                    @Override
                    public void _onNext(ResponseData<LoginBean> loginBeanResponseData) {
                        getView().loginSuccess(loginBeanResponseData);
                        getView().hideProgress();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        getView().loginFail();
                        getView().hideProgress();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        getView().hideProgress();
                    }
                });
    }

    public void toCodeLogin(String phoneNumber, String verification) {
        ApiService.codeLogin(phoneNumber,verification, 2)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<LoginBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        getView().showProgress("正在登录...");
                    }

                    @Override
                    public void _onNext(ResponseData<LoginBean> loginBeanResponseData) {
                        getView().loginSuccess(loginBeanResponseData);
                        getView().hideProgress();
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        getView().loginFail();
                        getView().hideProgress();
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        getView().hideProgress();
                    }
                });
    }

    public void getVCode(String phoneNumber) {
        ApiService.getVCode(phoneNumber)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<String>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                    }

                    @Override
                    public void _onNext(ResponseData<String> str) {
                        getView().codeNum(str.data);
                    }

                    @Override
                    public void _onError(String errorMessage) {
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                    }
                });
    }



    /**
     * 获取验证码
     */
    public void getCode(String phone){
        getVCode(phone);
        codeNum = 60;
        handler.postDelayed(runnable, CODE_TIME);
    }

    public void clear(){
//        getView().getCodeEnd();
        handler.removeCallbacks(runnable);
    }
}
